package com.example.budget3.months;


import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.budget3.R;
import com.example.budget3.util.UtilMethods;

public class MonthSpinnerAdapter extends BaseAdapter {
	
	// Text on buttons
    private final String mButtonNames [];           

    private final LayoutInflater mInflater;

    // The current selected event&#39;s time, used to calculate the date and day of the week
    // for the buttons.
    private long mMilliTime;

    private final Context mContext;

    public MonthSpinnerAdapter(Context context, Calendar latest) {
        super();
        mMilliTime = latest.getTimeInMillis();
        mContext = context;
        mButtonNames = context.getResources().getStringArray(R.array.buttons_list);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return  4;
    }

    @Override
    public Object getItem(int position) {
        if (position < mButtonNames.length) {
            return mButtonNames[position];
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        // Item ID is its location in the list
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v;
        // Check if can recycle the view
        if (convertView == null || 
        		((Integer) convertView.getTag()).intValue() != R.layout.pulldown_month_selector_button) {
            v = mInflater.inflate(R.layout.pulldown_month_selector_button, parent, false);
            v.setTag(Integer.valueOf(R.layout.pulldown_month_selector_button));
        } else {
            v = convertView;
        }
        TextView date = (TextView) v.findViewById(R.id.period_button);
        date.setText(buildMonthDate());

        return v;
    }

    @Override
    public boolean isEmpty() {
        return (mButtonNames.length == 0);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
    	if(position < 3){
	        View v = mInflater.inflate(R.layout.pulldown_month_selector_item, parent, false);
	        TextView viewType = (TextView)v.findViewById(R.id.period_item);
	        TextView date = (TextView)v.findViewById(R.id.date_text);
	        switch (position) {
	            case 0:
	                viewType.setText(mButtonNames [0]);
	                date.setText(buildMonthDayDate(0));
	                break;
	            case 1:
	                viewType.setText(mButtonNames [1]);
	                date.setText(buildMonthDayDate(1));
	                break;
	            case 2:
	                viewType.setText(mButtonNames [2]);
	                date.setText("");
	                break;
	            default:
	                v = convertView;
	                break;
	        }
	        return v;
    	}
    	return new View(mContext);
        
    }

    // Update the date that is displayed on buttons
    // Used when the user selects a new day/week/month to watch
    public void setTime(long time) {
        mMilliTime = time;
        notifyDataSetChanged();
    }

    private String buildMonthDayDate(int minus) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH,-1* minus);
        return UtilMethods.getMonthNameAndYear(cal);
    }

    private String buildMonthDate() {	
    	Calendar cal = Calendar.getInstance();
    	cal.setTimeInMillis(mMilliTime);
    	return UtilMethods.getMonthNameAndYear(cal);
    }

}
